﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RotovapPlatform.Domain.Business.Recipe.Enums;
using RotovapPlatform.Domain.Business.Recipes.Entities.Parameters;
using RotovapPlatform.Domain.Business.Machine.Enums;
using System.Collections.ObjectModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Text.Json.Serialization;
using static RotovapPlatform.Domain.Business.Machine.Enums.ExperimentConfigEnums;

namespace RotovapPlatform.Domain.Business.Recipes.Entities
{
	/// <summary>
	/// 实验实体类，用于表示一个具体的实验执行实例
	/// </summary>
	public class Experiment
	{
		/// <summary>
		/// 实验编号
		/// </summary>
		[Key]
		public Guid ExperimentId { get; set; }

		/// <summary>
		/// 关联的配方定义
		/// </summary>
		public RecipeDefinition Recipe { get; set; }

		/// <summary>
		/// 配方ID（外键）
		/// </summary>
		public Guid RecipeId { get; set; }

		/// <summary>
		/// 实验状态
		/// </summary>
		public ExperimentStatus Status { get; set; } = ExperimentStatus.Pending;

		/// <summary>
		/// 当前实验阶段
		/// </summary>
		public ExperimentStage CurrentStage { get; set; } = ExperimentStage.NotStarted;

		/// <summary>
		/// 当前实验子阶段
		/// </summary>
		public ExperimentSubStage CurrentSubStage { get; set; } = ExperimentSubStage.NotStarted;

		/// <summary>
		/// 实验优先级
		/// </summary>
		public ExperimentPriority Priority { get; set; } = ExperimentPriority.Normal;

		/// <summary>
		/// 实验执行开始时间
		/// </summary>
		public DateTime? ExecutionStartTime { get; set; }

		/// <summary>
		/// 实验执行完成时间
		/// </summary>
		public DateTime? ExecutionEndTime { get; set; }

		/// <summary>
		/// 实验执行结果
		/// </summary>
		public string ExecutionResult { get; set; } = string.Empty;

		/// <summary>
		/// 实验执行进度（0-100）
		/// </summary>
		public double ExecutionProgress { get; set; }

		/// <summary>
		/// 实验执行错误信息
		/// </summary>
		public string ErrorMessage { get; set; }

		/// <summary>
		/// 实验执行人员
		/// </summary>
		public string Executor { get; set; }

		/// <summary>
		/// 实验所属实验组ID
		/// </summary>
		public Guid ExperimentGroupId { get; set; }

		/// <summary>
		/// 实验在组内的执行顺序
		/// </summary>
		public int ExecutionOrder { get; set; }

		/// <summary>
		/// 实验参数（可能在执行过程中被修改，因此需要独立于配方）
		/// </summary>
		public RotovapParameters ExecutionParameters { get; set; }

		/// <summary>
		/// 从配方定义创建实验实例
		/// </summary>
		/// <param name="recipe">配方定义</param>
		/// <param name="experimentGroupId">实验组ID</param>
		/// <param name="executionOrder">执行顺序</param>
		/// <returns>实验实例</returns>
		public static Experiment FromRecipe(RecipeDefinition recipe, Guid experimentGroupId, int executionOrder)
		{
			if (recipe == null)
				throw new ArgumentNullException(nameof(recipe));

			// 添加调试日志
			System.Diagnostics.Debug.WriteLine($"FromRecipe: recipe.RotovapParameters = {(recipe.RotovapParameters == null ? "null" : "有值")}");

			// 确保RotovapParameters不为null，如果为null则创建一个新的实例
			var clonedParams = recipe.RotovapParameters?.Clone() ?? new RotovapParameters();
			System.Diagnostics.Debug.WriteLine($"FromRecipe: clonedParams = {(clonedParams == null ? "null" : "有值")}");

			// 添加更详细的调试信息
			if (clonedParams != null)
			{
				System.Diagnostics.Debug.WriteLine($"  MaterialName = {clonedParams.MaterialName}");
				System.Diagnostics.Debug.WriteLine($"  MaterialLocations.Count = {clonedParams.MaterialLocations?.Count ?? 0}");
				System.Diagnostics.Debug.WriteLine($"  ProductParameters.Count = {clonedParams.ProductParameters?.Count ?? 0}");
			}

			return new Experiment
			{
				ExperimentId = Guid.NewGuid(),
				Recipe = recipe,
				RecipeId = recipe.RecipeId,
				ExecutionParameters = clonedParams,
				Status = ExperimentStatus.Pending,
				Priority = ExperimentPriority.Normal,
				ExecutionProgress = 0,
				ExperimentGroupId = experimentGroupId,
				ExecutionOrder = executionOrder + 1,
				ExecutionResult = string.Empty,
				ErrorMessage = string.Empty,
				Executor = string.Empty
			};
		}

		/// <summary>
		/// 获取实验名称
		/// </summary>
		public string ExperimentName => Recipe?.ExperimentName;

		/// <summary>
		/// 获取实验描述
		/// </summary>
		public string ExperimentDescription => Recipe?.ExperimentDescription;

		/// <summary>
		/// 获取实验类型
		/// </summary>
		public RecipeEnums.ExperimentType ExperimentType => Recipe?.ExperimentType ?? default;

		/// <summary>
		/// 获取配方创建时间
		/// </summary>
		public DateTime RecipeCreatedTime => Recipe?.CreatedTime ?? DateTime.MinValue;

		/// <summary>
		/// 获取配方创建人员
		/// </summary>
		public string RecipeOperator => Recipe?.Operator;

		/// <summary>
		/// 获取配方ID
		/// </summary>
		public Guid GetRecipeId() => Recipe?.RecipeId ?? Guid.Empty;
	}

	/// <summary>
	/// 实验状态枚举
	/// </summary>
	public enum ExperimentStatus
	{
		/// <summary>
		/// 待执行
		/// </summary>
		Pending,

		/// <summary>
		/// 执行中
		/// </summary>
		Running,

		/// <summary>
		/// 已完成
		/// </summary>
		Completed,

		/// <summary>
		/// 已取消
		/// </summary>
		Cancelled,

		/// <summary>
		/// 执行失败
		/// </summary>
		Failed,

		/// <summary>
		/// 已暂停
		/// </summary>
		Paused
	}

	/// <summary>
	/// 实验优先级枚举
	/// </summary>
	public enum ExperimentPriority
	{
		/// <summary>
		/// 低优先级
		/// </summary>
		Low,

		/// <summary>
		/// 普通优先级
		/// </summary>
		Normal,

		/// <summary>
		/// 高优先级
		/// </summary>
		High,

		/// <summary>
		/// 紧急优先级
		/// </summary>
		Urgent
	}
}